﻿package net.ycsk;

import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.EditText;

 
import net.ycsk.model.newsModel;  // 
import net.ycsk.bll.newsBLL;    //引用业务对象 

public class newsList extends ListActivity implements OnMenuItemClickListener
{ 
    private newsBLL bll;
	private static final int REQUEST = 0;	
	private Integer ID = 0;
	public static ContactAdapter contactAdapter;

	//构造函数
	public newsList()
	{
		//---创建业务对象		
		bll = new newsBLL(this);
	}
 
	//内部类
	public class ContactAdapter extends BaseAdapter
	{

		public final class ViewHolder{  			
						public TextView etID; 
			public TextView etTitle; 
			public TextView etContent; 
			public TextView etHits; 
			public TextView dpAddDate; 
			public TextView etClassID; 
			public TextView etState; 
			public TextView etAuthor; 

		}  
		 
		public static final int RES_TEXT_ID = 1;  
		public static final int RES_BADGE_ID = 0;  
		private LayoutInflater layoutInflater;
		private List<newsModel> mData;  
		
		@Override  
		public int getCount() {  
	         return mData.size();  
	    }  	   
	     @Override  
	     public Object getItem(int arg0) {  
	        return mData.get(arg0);  
	     }  	   
	     @Override  
	     public long getItemId(int arg0) {  
	    	  return 0;  
	     }    
		 //构造函数
	    public ContactAdapter(Context context, List<newsModel>  list, boolean autoRequery)
		{	 
	    	 layoutInflater = LayoutInflater.from(context);  
	    	 mData = list; 
		}
	     
	     @Override
	     public View getView(int position, View convertView, ViewGroup parent) {
	    	 
	    	 ViewHolder holder = null;
	    	if (convertView == null) {
	    		holder = new ViewHolder(); 
	    		convertView = layoutInflater.inflate(R.layout.newslist_item, null);//newslist_item
					    	
					    		  holder.etID = (TextView)convertView.findViewById(R.id.newsList_etID); 
	    		  holder.etTitle = (TextView)convertView.findViewById(R.id.newsList_etTitle); 
	    		  holder.etContent = (TextView)convertView.findViewById(R.id.newsList_etContent); 
	    		  holder.etHits = (TextView)convertView.findViewById(R.id.newsList_etHits); 
	    		  //holder.dpAddDate = (TextView)convertView.findViewById(R.id.newsList_dpAddDate); 
	    		  holder.etClassID = (TextView)convertView.findViewById(R.id.newsList_etClassID); 
	    		  holder.etState = (TextView)convertView.findViewById(R.id.newsList_etState); 
	    		  holder.etAuthor = (TextView)convertView.findViewById(R.id.newsList_etAuthor); 


	    		convertView.setTag(holder);
			} else {
				holder = (ViewHolder)convertView.getTag();
			}
	    	 newsModel model = mData.get(position);	    	
			
             			holder.etID.setText(model.getID().toString());
			holder.etTitle.setText(model.getTitle());
			holder.etContent.setText(model.getContent());
			holder.etHits.setText(model.getHits().toString());
			//holder.dpAddDate.setText(model.getAddDate());
			holder.etClassID.setText(model.getClassID().toString());
			holder.etState.setText(model.getState());
			holder.etAuthor.setText(model.getAuthor());


	         return convertView;
	     }	     
	    
		/*
		 private void setChildView(ViewHolder holder, newsModel model) {

	    	 			holder.etID.setText(model.getID().toString());
			holder.etTitle.setText(model.getTitle());
			holder.etContent.setText(model.getContent());
			holder.etHits.setText(model.getHits().toString());
			//holder.dpAddDate.setText(model.getAddDate());
			holder.etClassID.setText(model.getClassID().toString());
			holder.etState.setText(model.getState());
			holder.etAuthor.setText(model.getAuthor());


	    	//绑定图片举例--------------
 			//ImageView ivPhone = (ImageView) view.findViewById(R.id.ivPhoto);		
 			//byte[] photo = cursor.getBlob(cursor.getColumnIndex("photo"));			
 			//ByteArrayInputStream bais = new ByteArrayInputStream(photo);
 			//ivPhone.setImageDrawable(Drawable.createFromStream(bais, "photo"));
	     }
		 */
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState); 
 
        List<newsModel> cursor = bll.selectAll(1);
        contactAdapter = new ContactAdapter(this, cursor, true);
		setListAdapter(contactAdapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuItem miAddContact = menu.add(1, 1, 1, "添加");
		MenuItem miEditContact = menu.add(1, 2, 2, "修改");
		MenuItem miDeleteContact = menu.add(1, 3, 3, "删除");
		miAddContact.setOnMenuItemClickListener(this);
		miEditContact.setOnMenuItemClickListener(this);
		miDeleteContact.setOnMenuItemClickListener(this);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	protected void onListItemClick(ListView listview, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(listview, v, position, id);
		//点击listview
		newsModel model = (newsModel) contactAdapter.getItem(position);
		ID = model.getID();//获取主键
	}

	@Override
	public boolean onMenuItemClick(MenuItem item)
	{
		Intent intent = null;
		switch (item.getItemId())
		{			
			case 1:// 添加记录
				intent = new Intent(this, newsForm.class);
				ID = 0;
				startActivityForResult(intent, REQUEST);
				break;			
			case 2:// 修改记录
				intent = new Intent(this, newsForm.class);					
				intent.putExtra("ID", ID);// 获取当前选择行的值		
				startActivityForResult(intent, REQUEST);
				break;			
			case 3:// 删除记录
				bll.delete(ID);
				// 刷新列表
				List<newsModel> list = bll.selectAll(1);
				contactAdapter = new ContactAdapter(this, list, true);
				setListAdapter(contactAdapter);
				break;
		}
		return false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {	
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != Activity.RESULT_OK) {
			return;
		}
		// 刷新列表
		List<newsModel> list = bll.selectAll(1);
		contactAdapter = new ContactAdapter(this, list, true);
		setListAdapter(contactAdapter);
	}

	@Override
	protected void onDestroy() {		
		//关掉db
		bll.closeDB();
		super.onDestroy();
	}
}